/**
 * Created by Administrator on 2016/8/11.
 */

define(["require",'config/appregister',"business/vedio/services/httpServices","business/services/constant","jquery","business/services/commentComp",
"business/vedio/dir/play","videoPlay","business/Companyinfor/service/chinese"],function(require,app,httpServices,constant){
    app.controller('videoPlay',function($scope,$http,$stateParams,$state,$rootScope,$location){

         $scope.switchDea=function(item){
         	$state.go("eng.vedioDetails",{courseId:item.courseId})
         }
         $scope.switchDea2=function(item){
         	$state.go("eng.vedioDetails",{courseId:item.courseId,hidePlay:1})
         }
         $scope.returnCompanyVideoList=function(){
         	 $state.transitionTo("comIndexCtrl.resource", {}, {
                    reload: false, inherit: false, notify: true
            });
            sessionStorage.ResourceTab = "3"
         }
    	$scope.tab=2
       	$rootScope.$on('$stateChangeSuccess', function(event, toState, toParams, fromState, fromParams) { 
  			
  			if(toState.controller == "videoPlay"){
  				location.reload();
  			}
  		}) 
        $scope.showMenu = function(){
            $(".videMenu").animate({
                    right:0
            }, 800 );
        }
		$scope.goTovideo=function(item){
			$state.go("eng.videoPlay", {videoPlayId:item.videoId,couseIdPlayId:$stateParams.couseIdPlayId})
		}
        $scope.go=function(){
            $state.go("eng.vedio")
        }
        $scope.golistOn=function(id){
            sessionStorage.topId = id
            sessionStorage.removeItem("twoId")
            sessionStorage.removeItem("thrId")
            $state.go("eng.vedio")
        }
        $scope.golistTw=function(id1,id2){
            sessionStorage.topId = id1
            sessionStorage.twoId = id2
            sessionStorage.removeItem("thrId")
            $state.go("eng.vedio")
        }
        $scope.golistTh=function(id1,id2,id3){
            sessionStorage.topId = id1
            sessionStorage.twoId = id2
            sessionStorage.thrId = id3
            $state.go("eng.vedio")
        }
        
        $scope.hideMenu = function(){
            $(".videMenu").animate({
                right:-538
            }, 800 );
        };
       function recentBrowse(){
			var req ={
				type:"GET",
				uri:"getEnginBrowse?count=8",
				data:{}
			};
			httpServices.getWorkData(req).success(function(response){
				$scope.$apply(function(){
					$scope.recentCon = response.data.enginBrowseList;//竟然设计成全局的浏览情况，可怕的产品
	
				})
			});
		}
		recentBrowse();
       
       $scope.goworkData=function(id){
			$state.go("eng.techdetail",{fileId:id})
		}
	
		$scope.LinkCourse=function(id){
			$state.go("eng.vedioDetails",{courseId:id})
		}
		$scope.goSkill=function(id){
			$state.go("eng.workDetailsCtrl",{skillId:id})
		}
		$scope.goanswer=function(id){
			$state.go("eng.postbardeta",{postbarId:id})
		}
		$scope.goNews = function(id){
			$state.go("eng.newsDetail",{newsId:id})
		}
       $scope.Linklibrary=function(id){
			$state.go("eng.libraryDetail",{thesisId:id})
		}
		$scope.Linkpostbar=function(id){
			$state.go("eng.libraryDetail",{thesisId:id})
		}
       /* function recentVideo(){//最近浏览的视频
            var req ={
                type:"GET",
                uri:"getBrowseVideo?pageNo=1&pageSize=10&all=false",
                data:{}
            };
            httpServices.getSameVideo(req).success(function(response){
                $scope.$apply(function(){
                    $scope.recentVideoCon = response.data.list;
                })
            });
        }*/
        /*相关视频*/
        function Samecourse(){
            var req ={
                type:"GET",
                uri:"getSameCourse",
                data:{
                	pageNo:1,
             		pageSize:4,
                }
            };
            httpServices.getSameVideo2(req).success(function(response){
                $scope.$apply(function(){
                    $scope.SameVideoCon = response.data.list;
                })
            });

        }
        Samecourse();
       /* recentVideo();*/
        /*有idji*/
		


        if($stateParams.couseIdPlayId && $stateParams.videoPlayId == null){
        	
          var req ={
                type:"GET",
                uri:"get?courseId=" + $stateParams.couseIdPlayId,
                data:{}
            };
            httpServices.getcourse(req).success(function(response){
                $scope.$apply(function(){
                    $scope.TopLink = response.data;
                    $scope.courseTit = response.data.course.title;
                    $scope.introduction = response.data.course.introduction;
                   
                    $scope.free = 0;
                    
                    getvideoDeatils();
                })
            });

			$scope.renderVideo = false ;
            function getvideoDeatils(){
                var req ={
                    type:"GET",
                    uri:"getStudyVideo",
                    data:{
                        courseId:$stateParams.couseIdPlayId,
                        freeStatus:$scope.free
                    }
                };
                httpServices.getSameVideo(req).success(function(response){
                    $scope.$apply(function(){
                    	$scope.renderVideo = true ;
                    	if(response.code==312){
                    		alert(response.msg)
                    	}else{
                    		$scope.videoInfo ={
							videoid:response.data.videoInfo.videoId,
							videoUrl:response.data.videoUrl,
							videoSubTitleInfo:response.data.videoSubTitleInfo
							}
                    	}
						 
                    })
                });
            }

            var videoId = $stateParams.couseIdPlayId
            function getCoure(){
                var req ={
                    type:"GET",
                    uri:"getCourseCatalog?courseId=" + videoId,
                    data:{}
                };
                httpServices.getchapter(req).success(function(response){
                    $scope.$apply(function(){
                        $scope.course = response.data;
                    })
                });
            }
            getCoure();

    }else if($stateParams.videoPlayId && $stateParams.couseIdPlayId){

            var req ={
                type:"GET",
                uri:"get?courseId=" + $stateParams.couseIdPlayId,
                data:{}
            };
            httpServices.getcourse(req).success(function(response){
                $scope.$apply(function(){
                    $scope.TopLink = response.data;
                    $scope.courseTit = response.data.course.title;
                    $scope.introduction = response.data.course.introduction;
                })
            });

            function getVIdeo(){
                var req ={
                    type:"GET",
                    uri:"get?videoId=" + $stateParams.videoPlayId,
                    data:{}
                };
                httpServices.getSameVideo(req).success(function(response){
                    $scope.$apply(function(){
                    	$scope.renderVideo = true ;
                    	if(response.code==609){
                    		alert("您未报名当前课程，请返回课程详情，报名课程")
                    		$state.go("eng.vedioDetails",{courseId:$stateParams.couseIdPlayId})
                    	}else{
                    		  $scope.videoInfo ={
								videoid:response.data.videoInfo.videoId,
								videoUrl:response.data.videoUrl,
								videoSubTitleInfo:response.data.videoSubTitleInfo
							}
                    	}
                       
                    })
                });

            }
            getVIdeo();
            var videoId = $stateParams.couseIdPlayId
            function getCoure(){
                var req ={
                    type:"GET",
                    uri:"getCourseCatalog?courseId=" + videoId,
                    data:{}
                };
                httpServices.getchapter(req).success(function(response){
                    $scope.$apply(function(){
                        $scope.course = response.data;
                    })
                });
            }
            getCoure();
       }
       else if($stateParams.videoPlayId && $stateParams.couseIdPlayId == null){
      $scope.showcons=1;
            function getVIdeo(){
                var req ={
                    type:"GET",
                    uri:"get?videoId=" + $stateParams.videoPlayId,
                    data:{}
                };
                httpServices.getSameVideo(req).success(function(response){
                    $scope.$apply(function(){
                    	if(response.code==610){
                    		alert(response.msg)
                    		 $state.transitionTo("comIndexCtrl.resource", {}, {
                                    reload: false, inherit: false, notify: true
				            });
				            sessionStorage.ResourceTab = "3"
				            
                    	}else{
                    		$scope.tab=1;
                    		 $scope.renderVideo = true ;
		                        $scope.videoInfo ={
									videoid:response.data.videoInfo.videoId,
									videoUrl:response.data.videoUrl,
									videoSubTitleInfo:response.data.videoSubTitleInfo
								}
                    	}
                       
                    })
                });

            }
            getVIdeo();
           
        }

     /*点击目录切换videourl*/
        $scope.videoUrlSwitch = function(item){
        	 $state.go("eng.videoPlay",{couseIdPlayId:item.chapterId})
       
        }
        //评论配置
        $scope.commentConf = {
            showStar: true,
            tbWidth:745,
            conf: {
                id: $stateParams.couseIdPlayId,
                type: 9
            }
        };



    })
})

